<template>
	<view style="background-color: #EFEFEF;height: 100vh;overflow: auto;">
		<u-tabs :list="list" :is-scroll="false" active-color="#EA4389" :current="current" @change="change"></u-tabs>
		<!-- <view class="message"><u-loadmore status="loadmore"/></view> -->
		<view v-if="current==0">
			<view class="coupons_box">
				
				<view class="coupons_item" v-for="(v,k) in datas.not_use.data" :key="k">
					<view class="item_left">
						<view class="money">{{ v.c_name }}</view>
						<view class="describe">.凭券可享受减{{ v.c_money }}元优惠</view>
						<view class="describe">.满{{ v.c_above_money }}元可用</view>
						<view class="describe">.有效期至{{ $u.timeFormat(v.c_endTime, 'yyyy年mm月dd日 hh:MM') }}</view>
						<view class="describe">.可与其他优惠活动同享</view>
					</view>
					
					<view class="item_right">
						<view class="money">￥{{ v.c_money }}</view>
						<view @click="just" class="butt">去使用</view>
					</view>
					
				</view>
				
			</view>
			
			<view v-if="datas.not_use.loading" class="tips_messgae">
				加载中...
			</view>
			<view @click="getData()" v-else-if="datas.not_use.error" class="tips_messgae">
				加载失败，点我重试
			</view>
			<view v-else-if="datas.not_use.end" class="tips_messgae">
				我也是有底线的
			</view>
			<view v-else-if="datas.not_use.data.length<=0" class="tips_messgae">暂无数据~</view>
		</view>
		
		<view v-else-if="current==1">
			<view class="coupons_box disabled" >
				
				<view class="coupons_item" v-for="(v,k) in datas.use.data" :key="k">
					<view class="item_left">
						<view class="money">{{ v.c_name }}</view>
						<view class="describe">.凭券可享受减{{ v.c_money }}元优惠</view>
						<view class="describe">.满{{ v.c_above_money }}元可用</view>
						<view class="describe">.有效期至{{ $u.timeFormat(v.c_endTime, 'yyyy年mm月dd日 hh:MM') }}</view>
						<view class="describe">.可与其他优惠活动同享</view>
					</view>
					
					<view class="item_right">
						<view class="money">￥{{ v.c_money }}</view>
						<view class="butt">已使用</view>
					</view>
					
				</view>
				
				<view v-if="datas.use.loading" class="tips_messgae">
					加载中...
				</view>
				<view @click="getData()" v-else-if="datas.use.error" class="tips_messgae">
					加载失败，点我重试
				</view>
				<view v-else-if="datas.use.data.length<=0" class="tips_messgae">暂无数据~</view>
				<view v-else-if="datas.use.end" class="tips_messgae">
					我也是有底线的
				</view>
				
			</view>
		</view>
		
		<view v-else-if="current==2">
			<view class="coupons_box disabled">
				
				<view class="coupons_item" v-for="(v,k) in datas.overdue.data" :key="k">
					<view class="item_left">
						<view class="money">{{ v.c_name }}</view>
						<view class="describe">.凭券可享受减{{ v.c_money }}元优惠</view>
						<view class="describe">.满{{ v.c_above_money }}元可用</view>
						<view class="describe">.有效期至{{ $u.timeFormat(v.c_endTime, 'yyyy年mm月dd日 hh:MM') }}</view>
						<view class="describe">.可与其他优惠活动同享</view>
					</view>
					
					<view class="item_right">
						<view class="money">￥{{ v.c_money }}</view>
						<view class="butt">已过期</view>
					</view>
					
				</view>
				
				<view v-if="datas.overdue.loading" class="tips_messgae">
					加载中...
				</view>
				<view @click="getData()" v-else-if="datas.overdue.error" class="tips_messgae">
					加载失败，点我重试
				</view>
				<view v-else-if="datas.overdue.data.length<=0" class="tips_messgae">暂无数据~</view>
				<view v-else-if="datas.overdue.end" class="tips_messgae">
					我也是有底线的
				</view>
				
			</view>
		</view>
		
		
	</view>
</template>
<!-- 往下拉显示内容 -->
<script>
	
	export default {
		data(){
			return {
				datas:{
					not_use:{
						where:{page:1,type:1},
						data:[],
						first:false,
						end:false,
						loading:false,
						error:false
					},
					use:{
						where:{page:1,type:2},
						data:[],
						first:true,
						end:false
					},
					overdue:{
						where:{page:1,type:3},
						data:[],
						first:true,
						end:false
					},
				},
				list: [
				{
					name: '未使用'
				},
				{
					name: '已使用'
				}, 
				{
					name: '已过期'
				}],
				current:0,
				key:'not_use'
			}
		},
		methods: {
			just(){
				uni.navigateTo({
					url:'/pages/user/my_member'
				})
			},
			change(index) {
				this.current = index;
				if(index==0){
					
					this.key = 'not_use'
					
				}else if(index==1){
					this.key = 'use'
				}else{
					this.key = 'overdue'
				}
				
				// alert(this.key)
				
				if(this.datas[this.key].first){
					
					this.datas[this.key].first = false
					
					this.getData()
				}
			},
			getData(){
				let where = this.datas[this.key].where
				
				console.log(this.key)
				
				console.log(this.datas[this.key].where)
				
				let that = this
				
				that.datas[this.key].loading = true
				
				that.datas[this.key].error = false
				
				let datas=JSON.parse(JSON.stringify(that.datas))
				this.$request({
					url:'/Coupons/list',
					data:where,
					method:'post',
					success(res){
						if(res.statusCode==200){
							
							if(res.data.code==1){
								
								let data = res.data.data.data
								
								for(var i in data){
									datas[that.key].data.push(data[i])
								}
								
								if(res.data.data.page>=res.data.data.allpage){
									
									datas[that.key].end = true
								}else{
									
									datas[that.key].where.page = res.data.data.page+1
									
								}
								
							}else{
								datas[that.key].error = true
							}
							
						}else{
							
							datas[that.key].error = true
							
						}
					},
					fail(err){
						datas[that.key].error = true
					},
					complete(){
						datas[that.key].loading = false
						
						that.datas = datas
						uni.stopPullDownRefresh()
					}
				})
			}
		},
		onLoad(){
			this.getData()
		},
		onPullDownRefresh(){
			this.datas[this.key].where.page=1
			this.datas[this.key].end = false
			this.datas[this.key].data = []
			this.getData()
		},
		onReachBottom(){
			if(this.datas[this.key].end){
				return false
			}
			
			this.getData();
		}
	}
</script>

<style scoped>
	view.coupons_box{
		display: flex;
		flex-direction: column;
		align-items: center;
	}
	view.coupons_box view.coupons_item{
		width:670rpx;
		padding: 20rpx;
		min-height: 220rpx;
		background-color: #fff;
		border-radius: 15rpx;
		margin-top: 20rpx;
		display: flex;
		font-size: 24rpx;
		justify-content: space-between;
	}
	view.coupons_box view.coupons_item view.item_left view.money{
		max-width: 360rpx;
		font-size: 34rpx;
		font-weight: bold;
		margin-bottom: 30rpx;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
	}
	view.disabled view.coupons_item view.item_left view.money{
		color:#888;
	}
	view.coupons_box view.coupons_item view.item_left view.describe{
		color:#888888;
		margin-top: 5rpx;
	}
	view.coupons_box view.coupons_item view.item_right .money{
		color:#EA4389;
		font-size: 30rpx;
		/* font-weight: bold; */
	}
	view.disabled view.coupons_item view.item_right .money{
		color: #888;
	}
	view.coupons_box view.coupons_item view.item_right {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
	view.coupons_box view.coupons_item view.item_right .butt{
		padding: 10rpx 30rpx;
		background-color: #EA4389;
		color:#fff;
		border-radius: 30rpx;
		margin-top: 15rpx;
	}
	view.disabled view.coupons_item view.item_right .butt{
		background-color: #888;
	}
</style>
